from django import template
from scenic.models import ScenicSpot
from django.core.cache import cache

register = template.Library()

@register.simple_tag
def get_random_spots(limit=4):
    cache_key = f'random_spots_{limit}'
    spots = cache.get(cache_key)
    if not spots:
        spots = list(ScenicSpot.objects.order_by('?')[:limit])
        cache.set(cache_key, spots, 60 * 15)  # 缓存15分钟
    return spots